package com.nest.WorkNest.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.lidroid.xutils.exception.HttpException;
import com.nest.WorkNest.R;
import com.nest.WorkNest.act.BulkOrderDetailAct;
import com.nest.WorkNest.act.LoginAct;
import com.nest.WorkNest.adapter.WaitReceiveAdapter;
import com.nest.WorkNest.api.XutilsPost;
import com.nest.WorkNest.tools.Tool;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class WaitReceive extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    private ListView myListView;
    private WaitReceiveAdapter adapter;
    private ArrayList<HashMap<String, String>> dataList = new ArrayList<>();
    private View view;
    private SwipeRefreshLayout mSwipeLayout;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.listview_bulkorder_waitreceive, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initClick();
    }

    private void initView() {
        loadBulkorderRecords();
        myListView = (ListView) this.getView().findViewById(R.id.wait_receive_list);
        mSwipeLayout = (SwipeRefreshLayout) this.getView().findViewById(R.id.rl_modulename_refresh);
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light, android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        adapter = new WaitReceiveAdapter(getActivity(), dataList);
        myListView.setAdapter(adapter);
        adapter.update(dataList);
    }

    private void initClick() {
        myListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), BulkOrderDetailAct.class);
                intent.putExtra("bulkOrderId", dataList.get(position).get("bulkOrderId"));
                intent.putExtra("mainImage", dataList.get(position).get("mainImage"));
                startActivity(intent);
            }
        });
    }

    public void loadBulkorderRecords() {
        XutilsPost xutilsPost = new XutilsPost();
        XutilsPost.IOAuthCallBack ioAuthCallBack = new XutilsPost.IOAuthCallBack() {
            @Override
            public void getIOAuthCallBack(String result) {
                JSONObject jsonObject = Tool.getJsonObject(result);
                JSONArray jsonArray;
                if (Tool.getString(jsonObject, "resultCode").equals("0")) {
                    jsonArray = Tool.getJsonArray(result, "bulkOrderList");
                    dataList = Tool.getListMapByJsonArray(jsonArray);
                    adapter.update(dataList);
                } else if (Tool.getString(result, "resultCode").equals("9001")) {
                    Toast.makeText(getActivity(), "token已过期，请重新登录", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getActivity(), LoginAct.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(getActivity(), Tool.getString(jsonObject, "MSG"), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void getDealException(HttpException httpException, String result) {

                Toast.makeText(getActivity(), "网络请求超时，请检查您的网络", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void getLoadingCallBack(long total, long current, boolean isUploading) {

            }
        };
        xutilsPost.doPostBulkorderRecords1(getActivity(), ioAuthCallBack);
    }

    @Override
    public void onResume() {
        loadBulkorderRecords();
        super.onResume();
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeLayout.setRefreshing(false);
                loadBulkorderRecords();
            }
        }, 2000);
    }
}
